﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace fingerpaint
{
    public partial class MainPage : PhoneApplicationPage
    {
        private Point point;
        private Point old_point;
        private bool draw = false;
        private string colour = "white";

        public MainPage()
        {
            InitializeComponent();

            old_point = point;
            colour = "blue";
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            blue.MouseLeftButtonDown += new MouseButtonEventHandler(selectColor);
            yellow.MouseLeftButtonDown += new MouseButtonEventHandler(selectColor);
            black.MouseLeftButtonDown += new MouseButtonEventHandler(selectColor);
            pink.MouseLeftButtonDown += new MouseButtonEventHandler(selectColor);

            paint.MouseMove += new MouseEventHandler(FingerMove);
            paint.MouseLeftButtonUp += new MouseButtonEventHandler(FingerUp);
            paint.MouseLeftButtonDown += new MouseButtonEventHandler(FingerDown);
        }

        void selectColor(object sender, MouseButtonEventArgs e)
        {
            Ellipse ellipse = sender as Ellipse;
            colour = ellipse.Name;
        }

        void FingerMove(object sender, MouseEventArgs e)
        {
            if (draw)
            {
                point = e.GetPosition(paint);
                Line line = new Line();

                if (colour == "blue")
                    line.Stroke = new SolidColorBrush(Color.FromArgb(255, 41, 159, 227));
                else if (colour == "yellow")
                    line.Stroke = new SolidColorBrush(Color.FromArgb(255, 226, 228, 43));
                else if (colour == "black")
                    line.Stroke = new SolidColorBrush(Color.FromArgb(255, 42, 42, 42));
                else if (colour == "pink")
                    line.Stroke = new SolidColorBrush(Color.FromArgb(255, 228, 29, 180));

                line.X1 = point.X;
                line.Y1 = point.Y;
                line.X2 = old_point.X;
                line.Y2 = old_point.Y;

                line.StrokeStartLineCap = PenLineCap.Round;
                line.StrokeEndLineCap = PenLineCap.Round;

                line.StrokeThickness = 15;
                line.Opacity = 0.5;

                paint.Children.Add(line);

                old_point = point;
            }
            old_point = point;
        }

        void FingerUp(object sender, MouseButtonEventArgs e)
        {
            draw = false;
        }

        void FingerDown(object sender, MouseButtonEventArgs e)
        {
            point = e.GetPosition(paint);
            old_point = point;
            draw = true;
        }

        private void btnDelete_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            paint.Children.Clear();
        }
    }
}